/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package knn;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Nguyen Thuy Ngoc <ntngoc1988@gmail.com>
 */
public class WeightedDominantSelector<T extends ISample> implements IDominantSelector<T> {

    @Override
    public String determineDominantClass(T[] samples, double[] distances) {
        //throw new UnsupportedOperationException("Not supported yet.");
        String dominantClass = null;
        Double dominantCount = 0.0;
        
        double core = distances[0];
        double[] weights = new double[distances.length];
        double tmp = 0.0;
        for(int i = 0; i < distances.length; i++){
            tmp = tmp + core / distances[i];
        }
        weights[0] = 1.0/tmp;
        for(int i = 1; i < weights.length; i++) {
            weights[i] = weights[0] * core / distances[i];
        }
        Map<String, Double> countPerClass = new HashMap<String, Double>();

        for(int i = 0; i < samples.length; i++) {
            if(countPerClass.containsKey(samples[i].getAssignedClass())) {
                Double count = countPerClass.get(samples[i].getAssignedClass()) + weights[i];
                countPerClass.put(samples[i].getAssignedClass(), count);
            }
            else {
                countPerClass.put(samples[i].getAssignedClass(), weights[i]);
            }
            if (countPerClass.get(samples[i].getAssignedClass()) > dominantCount) {
                dominantClass = samples[i].getAssignedClass();
                dominantCount = countPerClass.get(samples[i].getAssignedClass());
            }
        }
        
        return dominantClass;
    }


}
